package com.pricy.me.model;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.UnknownHostException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.pricy.me.R;


public class ProductExtended extends Product {
	protected boolean bonneaffaire;
	protected int nbrbonneaffaire;
    protected boolean jelai;
    protected int nbrjelai;
    
    public boolean isBonneAffaire() {
		return bonneaffaire;
	}

	public void setBonneAffaire(boolean bonneaffaire) {
		this.bonneaffaire = bonneaffaire;
	}

	public boolean isJelai() {
		return jelai;
	}

	public void setJelai(boolean jelai) {
		this.jelai = jelai;
	}

	public int getNbrbonneaffaire() {
		return nbrbonneaffaire;
	}

	public void setNbrbonneaffaire(int nbrbonneaffaire) {
		this.nbrbonneaffaire = nbrbonneaffaire;
	}
	
	public void incNbrbonneaffaire() {
		this.nbrbonneaffaire++;
	}
	public void decNbrbonneaffaire() {
		this.nbrbonneaffaire--;
	}
	
	public int getNbrjelai() {
		return nbrjelai;
	}

	public void setNbrjelai(int nbrjelai) {
		this.nbrjelai = nbrjelai;
	}
	
	public void incJelai() {
		this.nbrjelai++;
	}
	public void decJelai() {
		this.nbrjelai--;
	}
	
	public void loadData(Context c, int id){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
		
		
		try{
			User us = new User();
			us.loadData(c);
			Log.e("URL",c.getResources().getString(R.string.lien)+"api/product/getbyid?id="+id+"&user="+us.getId());
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(c.getResources().getString(R.string.lien)+"api/product/getbyid?id="+id+"&user="+us.getId());
			HttpResponse response = httpclient.execute(httppost);
			
			String reponse = EntityUtils.toString(response.getEntity());
			Log.e("REPONSE", reponse);
			
			ProductExtended p = gson.fromJson(reponse, ProductExtended.class);
			JsonParser parser = new JsonParser();
			JsonObject object = parser.parse(reponse).getAsJsonObject();
			User u = gson.fromJson(object.getAsJsonObject().get("user"), User.class);
        	Category cat = gson.fromJson(object.getAsJsonObject().get("category"), Category.class);
        	
			this.adress = p.getAdress();
			this.bonneaffaire = p.isBonneAffaire();
			this.by = p.getBy();
			this.category = cat;
			this.currency = p.getCurrency();
			this.date = p.getDate();
			this.description = p.getDescription();
			this.id = p.getId();
			this.jelai = p.isJelai();
			this.latitude = p.getLatitude();
			this.longitude = p.getLongitude();
			this.nbrbonneaffaire = p.getNbrbonneaffaire();
			this.nbrjelai = p.getNbrjelai();
			this.owner = p.isOwner();
			this.photo = p.getPhoto();
			this.price = p.getPrice();
			this.quality = p.getQuality();
			this.title = p.getTitle();
			this.user = u;
			this.views = p.getViews();
			this.visiblity = p.getVisiblity();
			
		} catch (UnsupportedEncodingException e) {
			Log.e("ERROR", e.toString());
		} catch (ClientProtocolException e) {
			Log.e("ERROR", e.toString());
		} catch (UnknownHostException e) {
			Log.e("ERROR", e.toString());
		} catch (IOException e) {
			Log.e("ERROR", e.toString());
		}
	}
		
}
